package com.huaxin.hxcloud.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.work.entity.DataItem;
import com.huaxin.hxcloud.work.entity.StationTarget;
import com.huaxin.hxcloud.work.entity.StationTargetDTO;
import com.huaxindata.common.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/1/23
 * @Desc $desc$
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxStationDataTypeActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.rv_station_target)
    RecyclerView rv_station_target;

    private String mStationCode;
    private int    mStationType;
    private String mStationName;

    ArrayList<MultiItemEntity> data = new ArrayList<>();
    private StationTargetExpandableItemAdapter mExpandableItemAdapter;
    private View                               emptyView;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_station_data;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        //Intent intent = getIntent();
        if (!extras.containsKey(Constants.IK_STATION_CODE)) {
            finish();
        }
        mStationCode = extras.getString(Constants.IK_STATION_CODE);
        mStationType = extras.getInt(Constants.IK_STATION_TYPE, 1);
        mStationName = extras.getString(Constants.IK_STATION_NAME);
        Log.i("lwjing", "stationcode= " + mStationCode + "  mStationName=" + mStationName);
    }

    @Override
    public void initView() {
        Log.i("lwjing", "initView mStationName: " + mStationName);
        mToolbar.setTitle(mStationName);
        setSupportActionBar(mToolbar);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        mExpandableItemAdapter = new StationTargetExpandableItemAdapter(data, this);
        rv_station_target.setAdapter(mExpandableItemAdapter);
        rv_station_target.setLayoutManager(manager);
        mExpandableItemAdapter.expandAll();

        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) rv_station_target.getParent(), false);


    }

    @Override
    protected void requestData() {
        // super.requestData();
        apiFindDataTypeByStationCode();
    }

    private void apiFindDataTypeByStationCode() {
        updateLoadStatus(LOADING);
        Call<StationTargetDTO> call = HxTransmitRequestHelper.getRequestApi().getFindDataTypeByStationCode(mStationCode);
        autoClose(call);
        call.enqueue(new Callback<StationTargetDTO>() {

            @Override
            public void onResponse(Call<StationTargetDTO> call, Response<StationTargetDTO> response) {
                StationTargetDTO stationTargetDTO = response.body();
                if (response.isSuccessful() && stationTargetDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<StationTarget> stationTarList = stationTargetDTO.getResult();
                    if (stationTarList != null && stationTarList.size() > 0) {
                        Log.i("lwjing", "stationtargetDto  size: " + stationTarList.size());
                        //数据初始化
                        updateView(stationTarList);
                    } else {
                        mExpandableItemAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<StationTargetDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    /**
     * recycleview 初始化
     *
     * @param stationTarList
     */
    private void updateView(List<StationTarget> stationTarList) {
        data.clear();
//        if (mStationType == 2) {
//            for (int i = 0; i < stationTarList.size(); i++) {
//                StationTarget stationTarget = stationTarList.get(i);
//                List<DataItem> dataItemList = stationTarget.getDataItemList();
//                if (dataItemList != null && dataItemList.size() > 0) {
//                    data.addAll(dataItemList);
//                }
//            }
//            if (data.size() == 0) {
//                //设置空布局
//                mExpandableItemAdapter.setEmptyView(emptyView);
//            } else {
//                mExpandableItemAdapter.setNewData(data);
//            }
//            return;
//        }

        for (int i = 0; i < stationTarList.size(); i++) {
            StationTarget stationTarget = stationTarList.get(i);
            List<DataItem> dataItemList = stationTarget.getDataItemList();
            if (dataItemList != null && dataItemList.size() > 0) {
                for (int j = 0; j < dataItemList.size(); j++)
                    stationTarget.addSubItem(dataItemList.get(j));
            }
            data.add(stationTarget);
        }
        Log.i("lwjing", "data size " + data.size());
        if (data.size() == 0) {
            //设置空布局
            mExpandableItemAdapter.setEmptyView(emptyView);
        } else {
            mExpandableItemAdapter.setNewData(data);
        }
    }

    class StationTargetExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
        public static final int INDUSTRIAL_ART = 0;
        public static final int RUN_STATA      = 1;
        private final Context mContext;

        public StationTargetExpandableItemAdapter(List<MultiItemEntity> data, Context context) {
            super(data);
            this.mContext = context;
            addItemType(INDUSTRIAL_ART, com.huaxin.common.R.layout.item_expandable_art);
            addItemType(RUN_STATA, com.huaxin.common.R.layout.item_expandable_state);

        }

        @Override
        protected void convert(final BaseViewHolder holder, MultiItemEntity multiItemEntity) {
            Log.i("lwjing", "convert: " + multiItemEntity.toString());
            switch (holder.getItemViewType()) {
                case INDUSTRIAL_ART:
                    final StationTarget stationTarget = (StationTarget) multiItemEntity;
                    holder.setText(com.huaxin.common.R.id.tv_art_name, stationTarget.getName());

                    if (stationTarget.isExpanded()) {
                        holder.setImageResource(R.id.iv_expand_arrow, R.drawable.icon_down);
                    } else {
                        holder.setImageResource(R.id.iv_expand_arrow, R.drawable.ic_arrow_right);
                    }

                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int pos = holder.getAdapterPosition();

                            if (stationTarget.getSubItems() == null || stationTarget.getSubItems().size() == 0) {
                                ToastUtil.showToast(HxStationDataTypeActivity.this, "没有相关指标项");
                                return;
                            }
                            if (stationTarget.isExpanded()) {
                                collapse(pos);
                                holder.setImageResource(R.id.iv_expand_arrow, R.drawable.icon_down);

                            } else {
                                expand(pos);
                                holder.setImageResource(R.id.iv_expand_arrow, R.drawable.ic_arrow_right);
                            }
                        }
                    });

                    break;
                case RUN_STATA:
                    final DataItem dataItem = (DataItem) multiItemEntity;
                    holder.setText(com.huaxin.common.R.id.tv_stata_name, dataItem.getName());
                    ImageView iv_icon = holder.getView(R.id.iv_icon);
                    if (mStationType == 2) {
                        iv_icon.setVisibility(View.VISIBLE);
                    } else {
                        iv_icon.setVisibility(View.INVISIBLE);
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(mContext, HxTargetRunStataActivity.class);
                            intent.putExtra(Constants.IK_STATION_CODE, mStationCode);
                            intent.putExtra(Constants.IK_STATION_TYPE, mStationType);
                            intent.putExtra(Constants.IK_STATION_NAME, mStationName);
                            intent.putExtra(Constants.IK_DATAITEM_ID, dataItem.getId());
                            mContext.startActivity(intent);
                        }
                    });
                    break;
            }
        }
    }


}
